package com.oreilly.ez.fileserver.base;

import com.oreilly.ez.fileserver.entity.FileInfo;
import com.oreilly.ez.fileserver.entity.SysUser;
import com.oreilly.ez.fileserver.service.impl.FileInfoService;
import com.oreilly.ez.fileserver.service.impl.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileFilter;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Component
@EnableScheduling
@EnableAsync
public class Scheduler {

    @Autowired
    private SysUserService userService;

    @Autowired
    private FileInfoService fileInfoService;

    @Autowired
    private YmlConfig ymlConfig;

    //每隔60s执行一次
    @Scheduled(fixedDelay = 60000)
    @Async
//    @Scheduled(cron = "0 */1 * * * ?")
    public void testTasks() {
        System.out.println("thread " + Thread.currentThread().getName());
        System.out.println("定时任务执行时间：" + System.currentTimeMillis());

        userService.find(new SysUser(), PageRequest.of(0,10));
    }



    @Scheduled(fixedDelay = 30*60000)
    public void cleanRedundantUploads() {
        System.out.println("thread " + Thread.currentThread().getName());
        System.out.println("remove redundant files in uploads start：" + System.currentTimeMillis());


//        {
//            sid: "ff8080816d80e3a4016d80f08ddc0012",
//                    aac001: "Reference App Usability Study Feedback from Club Car.pdf",
//                aac002: "file-6273103012838634512.pdf",
//                aac003: "new-upload",
//                ctime: "2019-09-30 14:51:52"
//        }
        Page<FileInfo> infoPage = fileInfoService.find(new FileInfo(), PageRequest.of(0, 1000));
        if(infoPage.getTotalElements() >0){
            List<FileInfo> content = infoPage.getContent();
            List<String> stringList = content.stream().map(ele -> ele.getAac002()).collect(Collectors.toList());

            // 文件上传路径
            File rootDir = new File(System.getProperty("user.dir"), ymlConfig.getUploadDir());

            File[] realFiles = rootDir.listFiles( (File pathname) -> pathname.getName().startsWith("file-") );

            if(realFiles ==null){
                return;
            }
            List<File> redundantFileList = Arrays.stream(realFiles).filter((file -> !stringList.contains(file.getName()))).collect(Collectors.toList());

            for (int i = redundantFileList.size() - 1; i >= 0; i--) {
                File file = redundantFileList.get(i);
                boolean delete = file.delete();
                System.out.println("-- " + file.getName());
            }

        }
    }

}
